<template>
  <div>
    <Tour v-model:current="current" v-model:show="show" :steps="steps" />
    <!--引用表格-->
    <BasicTable ref="basicTable" @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined" ref="addBtn">{{ common('addText') }}</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> {{ common('exportText') }}</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls"> {{ common('importText') }}</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                {{ common('delText') }}
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >{{ common('batchOperation') }}
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <AssetInfoModal ref="assetInfoModal" @register="registerModal" @success="handleSuccess"></AssetInfoModal>
    <PrintModal @register="registerModal2" />
    <!-- 弹窗区域 -->
  </div>
</template>

<script lang="ts" name="asset-info-assetInfo" setup>
  import { ref, onMounted, h, nextTick } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import AssetInfoModal from './components/AssetInfoModal.vue';
  import { columns, searchFormSchema } from './AssetInfo.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './AssetInfo.api';
  import { Tour, type TourStep } from '@/components/Tour/index';
  import PrintModal from './components/PrintModal.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t: common } = useI18n('common');
  const { t } = useI18n('asset.assetInfo');

  // const checkedKeys = ref<Array<string | number>>([]);
  //注册model
  const [registerModal, { openModal, closeModal }] = useModal();

  const [registerModal2, { openModal: openModal2 }] = useModal();
  //注册table数据
  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: t('title'),
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 180,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: t('title'),
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  const basicTable = ref<InstanceType<typeof BasicTable>>();

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  function handlePrint(record: Recordable) {
    console.log(record.id, record, 'record.id');
    openModal2(true, {
      assetId: record.id,
      showFooter: false,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: common('editText'),
        onClick: handleEdit.bind(null, record),
      },
      {
        label: common('print'),
        onClick: handlePrint.bind(null, record),
      },
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: common('detailText'),
        onClick: handleDetail.bind(null, record),
      },
      {
        label: common('delText'),
        popConfirm: {
          title: common('delTip'),
          placement: 'left',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
  const addBtn = ref();
  const assetInfoModal = ref<InstanceType<typeof AssetInfoModal>>();
  const show = ref(false);
  const current = ref(0);
  const steps = ref<TourStep[]>([
    {
      el: () => addBtn.value.$el,
      title: common('addText'),
      message: t('tour.step1Msg'),
      placement: 'right',
      next: (next) => {
        handleAdd();
        nextTick(() => {
          next();
        });
      },
    },
    {
      el: () => assetInfoModal.value?.basicForm?.$el,
      title: t('addText'),
      message: h('div', {}, [t('tour.step2Msg1'), h('span', { style: 'color:red;' }, ' * '), t('tour.step2Msg2')]),

      prev: (prev) => {
        closeModal();
        prev();
      },
    },
    {
      el: () => assetInfoModal.value?.basicModal?.modalFooter?.$el,
      title: t('tour.step3Title'),
      message: t('tour.step3Msg'),
      next: (next) => {
        closeModal();
        next();
      },
    },
    {
      el: () => basicTable.value?.tableElRef?.$el,
      title: t('tour.step4Title'),
      message: t('tour.step4Msg'),
      prev: (prev) => {
        handleAdd();
        nextTick(() => {
          prev(700);
        });
      },
    },
  ]);
  onMounted(() => {
    setTimeout(() => {
      // show.value = true;
    }, 500);
  });
</script>

<style scoped></style>
